package com.kycxika.app.activity.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.kycxika.app.R;
import com.kycxika.app.adapter.FreeExperienceAdapter;
import com.kycxika.app.view.RefreshLayout;
import com.lynnchurch.horizontalscrollmenu.HorizontalScrollMenu;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.ArrayList;
import java.util.List;

import static android.provider.Contacts.SettingsColumns.KEY;

/**
 * Created by kyc on 2017/5/2.
 */
public class HomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener,
        RefreshLayout.OnLoadListener, ViewPager.OnPageChangeListener {

    private View mView;
    private HorizontalScrollMenu hsm_container;
    private LinearLayout mLeft;
    private LinearLayout mRight;

    private String[] imagePathStr = {""};
    private String downLoadUrl = "";
    private String apkName = "";

    private ListView mStar_list;
    private RefreshLayout swipeLayout;
    private ArrayList<String> list;
    private FreeExperienceAdapter mAdapter1;
    //private ViewPager viewPager;
    private List<ImageView> list_view = new ArrayList<>();

    // 底部小点图片
    private ImageView[] dots;
    private int currentIndex;
    private ViewPager viewpager;
    private LinearLayout pointgroup;
    private TextView txt_name;

    private String[] titlePage = {"测试图片一","测试图片二","测试图片三","测试图片四"};
    private int[] ImagePage = {R.drawable.ce_01,R.drawable.ce_02,R.drawable.ce_03,R.drawable.ce_04};


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.fragment_home, null);
        initView();
        // setData();
        setListener();
        return mView;
    }

    /**
     * 添加数据
     */
    private void setData() {
        list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("数据" + i);
        }
       /* listView = (ListView) findViewById(R.id.list);
        listView.addHeaderView(header);
        adapter = new MyAdapter(this, list);
        listView.setAdapter(adapter);*/
    }

    public void initView() {

        list_view.clear();
        viewpager = (ViewPager) mView.findViewById(R.id.viewpager);
        txt_name = (TextView) mView.findViewById(R.id.txt_name);
        pointgroup = (LinearLayout) mView.findViewById(R.id.pointgroup);

        PercentLinearLayout.LayoutParams layouts = new PercentLinearLayout.LayoutParams(
                new PercentLinearLayout.LayoutParams(PercentLinearLayout.LayoutParams.MATCH_PARENT, PercentLinearLayout.LayoutParams.MATCH_PARENT));
        for (int i = 0; i < ImagePage.length; i++) {
            ImageView image = new ImageView(getContext());
            image.setImageResource(ImagePage[i]);
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            image.setLayoutParams(layouts);
            list_view.add(image);
        }

        dots = new ImageView[list_view.size()];
        for (int i = 0; i < dots.length; i++) {
            ImageView dot = new ImageView(getContext());
            dot.setImageResource(R.drawable.circle_white);
            dots[i] = dot;
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            layoutParams.leftMargin = 5;
            layoutParams.rightMargin = 5;
            pointgroup.addView(dots[i], layoutParams);
        }
        currentIndex = 0;
        dots[currentIndex].setImageResource(R.drawable.circle_selector);// 设置为选中状态
        txt_name.setText(titlePage[currentIndex]);
        viewpager.setOnPageChangeListener(this);

        ViewPagerAdapter adapter = new ViewPagerAdapter(list_view);
        viewpager.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void onLoad() {
    }

    @Override
    public void onRefresh() {
    }


    /**
     * 设置监听
     */
    private void setListener() {
       /* swipeLayout.setOnRefreshListener(this);
        swipeLayout.setOnLoadListener(this);*/
    }

    // 当滑动状态改变时调用
    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    // 当当前页面被滑动时调用
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    // 当新的页面被选中时调用
    @Override
    public void onPageSelected(int arg0) {
        // 设置底部小点选中状态
        setCurrentDot(arg0);
    }

    private void setCurrentDot(int position) {
        if (position < 0 || position > list_view.size() - 1
                || currentIndex == position) {
            return;
        }
        dots[position].setImageResource(R.drawable.circle_selector);
        txt_name.setText(titlePage[position]);
        dots[currentIndex].setImageResource(R.drawable.circle_white);
        currentIndex = position;
    }

    public static HomeFragment newInstance(String mTitle) {
        HomeFragment fragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY, mTitle);
        fragment.setArguments(bundle);
        return fragment;
    }

    private class ViewPagerAdapter extends PagerAdapter {
        private final List<ImageView> views;

        public ViewPagerAdapter(List<ImageView> list_view) {
            this.views = list_view;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //((ViewPager) container).removeView(views.get(position));
            container.removeView(views.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // 将当前视图添加到container中
            //((ViewPager) container).addView(views.get(position));
            View v=views.get(position);
            ViewGroup parent = (ViewGroup) v.getParent();
            if (parent != null) {
                parent.removeAllViews();
            }
            container.addView(views.get(position));
            // 设置当前视图的唯一标示Key
            return views.get(position);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
       // mHandler.postDelayed(new ViewPageTask(), 5000);
    }

    @Override
    public void onStop() {
        super.onStop();
        //mHandler.removeCallbacks(new ViewPageTask());
    }

    @Override
    public void onResume() {
        super.onResume();
        mHandler.postDelayed(new ViewPageTask(), 5000);
    }

    @Override
    public void onPause() {
        super.onPause();
        mHandler.removeCallbacks(new ViewPageTask());
    }

    private int currentItem;

    /**
     * 图片轮播任务
     *
     * @author liuyazhuang
     */
    class ViewPageTask implements Runnable {

        @Override
        public void run() {
            if(currentIndex == list_view.size() - 1) {
                currentItem = 0;
            }else {
                currentItem = (currentItem + 1) % list_view.size();
            }
            mHandler.sendEmptyMessage(0);
        }
    }

    /**
     * 接收子线程传递过来的数据
     */
    private Handler mHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            if(currentIndex == list_view.size() - 1) {
                viewpager.setCurrentItem(currentItem,false);
            }else {
                viewpager.setCurrentItem(currentItem);
            }
            setCurrentDot(currentItem);
            mHandler.postDelayed(new ViewPageTask(), 5000);
        };
    };


}
